package com.guaju.yueshangclothing.fragment;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.guaju.yueshangclothing.R;
import com.guaju.yueshangclothing.adapter.MyFragmentPagerAdapter;
import com.guaju.yueshangclothing.base.BaseFragment;
import com.guaju.yueshangclothing.databinding.FragmentHomeBinding;
import com.guaju.yueshangclothing.fragment.home.RecommendFragment;
import com.guaju.yueshangclothing.fragment.home.TotalGoodsFragment;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * Created by guaju on 2018/9/20.
 */

public class HomeFragment extends BaseFragment {

    FragmentHomeBinding binding;
    private ArrayList<String> mTitleList = new ArrayList<>(2);
    private ArrayList<Fragment> mFragments = new ArrayList<>(2);

    @Override
    public int setContent() {
        return R.layout.fragment_home;
    }


    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        binding = (FragmentHomeBinding) bindingView;
        showLoading();
        initFragmentList();
        /**
         * 注意使用的是：getChildFragmentManager，
         * 这样setOffscreenPageLimit()就可以添加上，保留相邻3个实例，切换时不会卡
         * 但会内存溢出，在显示时加载数据
         */
        MyFragmentPagerAdapter myAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragments, mTitleList);

        binding.vpHome.setAdapter(myAdapter);
        // 左右预加载页面的个数
        binding.vpHome.setOffscreenPageLimit(mTitleList.size() - 1);
        myAdapter.notifyDataSetChanged();
        binding.tablayoutHome.setTabMode(TabLayout.MODE_FIXED);
        reflex(binding.tablayoutHome);
        ((FragmentHomeBinding) bindingView).tablayoutHome.setupWithViewPager(binding.vpHome);
        showContentView();
        // item点击跳转
//        initRxBus();
    }

    //设置tablayout的tab宽度包裹内容
    public void reflex(final TabLayout tabLayout) {
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    Field mTabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
                    mTabStripField.setAccessible(true);

                    LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(tabLayout);

                    int dp10 = dip2px(tabLayout.getContext(), 10);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

    }


    public int dip2px(Context context, float dipValue) {

        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


    private void initFragmentList() {
        mTitleList.clear();
        mTitleList.add("推荐");
        mTitleList.add("全部商品");
        mTitleList.add("大衣");
        mTitleList.add("派克");
        mTitleList.add("夹克");
        mTitleList.add("羊绒系列");
//        mTitleList.add("大安卓");
        mFragments.add(RecommendFragment.newInstance("推荐"));
        mFragments.add(TotalGoodsFragment.newInstance("全部商品"));
        mFragments.add(TotalGoodsFragment.newInstance("大衣"));
        mFragments.add(TotalGoodsFragment.newInstance("派克"));
        mFragments.add(TotalGoodsFragment.newInstance("夹克"));
        mFragments.add(TotalGoodsFragment.newInstance("羊绒系列"));
//        mFragments.add(new CustomFragment());
//        mFragments.add(AndroidFragment.newInstance("Android"));
    }


}
